package com.mycookbook;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class ModifyRecipeActivity extends Activity {
	private DatabaseHandler db;
	private ArrayList<String> listOfIngredients;
	private String modify;
	
	private int id;
	
	Uri imageUri;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.modifyrecipelayout);
		
		listOfIngredients = new ArrayList<String>();
		
		//Setting up the database
        ApplicationData d = ((ApplicationData)getApplicationContext());
        db = d.GetDatabase();
		
		//Setting title according to edit or create
		Intent intent = getIntent();
		modify = intent.getStringExtra("modify");

		final TextView titleTextView = (TextView) findViewById(R.id.txtTitle); 
		final Button modifyOrCreateButton = (Button) findViewById(R.id.btnCreate); 
		
		if(modify.equals("ModifyRecipe")){
			titleTextView.setText("Edit recipe");
			modifyOrCreateButton.setText("Update");
			
			final EditText nameTextView = (EditText) findViewById(R.id.editTxtName);
			final EditText approachTextView = (EditText) findViewById(R.id.editTxtApproach);
			final ListView ingredientsTextView = (ListView) findViewById(R.id.listViewIngredients);
			final RadioGroup radiogroup = (RadioGroup) findViewById(R.id.radioGroupCategory);
			
			//Extract data from recipe
			String modify = intent.getStringExtra("modify");
			imageUri = Uri.parse(intent.getStringExtra("imageUri"));
			String name = intent.getStringExtra("name");
			String approach = intent.getStringExtra("approach");
			String category = intent.getStringExtra("category");
			id = Integer.parseInt(intent.getStringExtra("id"));
			
			//Set controls using the data
			nameTextView.setText(name);
			
			listOfIngredients = new ArrayList(Arrays.asList(db.getIngredientsAsArray(name)));
	        final ArrayAdapter<String> aa;
	        aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listOfIngredients);
	        ingredientsTextView.setAdapter(aa);
			
	        approachTextView.setText(db.getApproach(name));
	        
	        final RadioButton radioAppetizer = (RadioButton) findViewById(R.id.radioAppetizer);
	        final RadioButton radioMain = (RadioButton) findViewById(R.id.radioMain);
	        final RadioButton radioDessert = (RadioButton) findViewById(R.id.radioDessert);
	        if(category.equals("Main Course"))
	        	radioMain.setChecked(true);
	        if(category.equals("Appetizer"))
	        	radioAppetizer.setChecked(true);
	        if(category.equals("Dessert"))
	        	radioDessert.setChecked(true);
	        
		}else{
			titleTextView.setText("Create new recipe");
			modifyOrCreateButton.setText("Create");
		}
		Log.i("LOL", "onCreate() Modify");
	}
	
	public void onClickCancel(View V){
		//Return to the proper layout according to create or update.
		if(modify.equals("ModifyRecipe")){
			
		}else{

		}
		Log.i("LOL", "onClickCancel() Modify");
	}
	
	public void onClickAddIngredient(View v){
		final ListView listViewIngredients = (ListView) findViewById(R.id.listViewIngredients);
        final ArrayAdapter<String> aa;
        
        EditText mEdit = (EditText)findViewById(R.id.editTxtIngredients);
        listOfIngredients.add(mEdit.getText().toString());
        mEdit.setText("");
        
        aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listOfIngredients);
        listViewIngredients.setAdapter(aa);
        //aa.notifyDataSetChanged();
        Log.i("LOL", "onClickAddIngredient() Modify");
	}
	
	public void onClickCreateOrUpdate(View V){
		Recipe r = new Recipe();
		Log.i("LOL", "onClickCreateOrUpdate() Modify");
		//Extracting layout data
		final EditText nameTextView = (EditText) findViewById(R.id.editTxtName);
		final EditText approachTextView = (EditText) findViewById(R.id.editTxtApproach);
		final ListView ingredientsTextView = (ListView) findViewById(R.id.listViewIngredients);
		final RadioGroup radiogroup = (RadioGroup) findViewById(R.id.radioGroupCategory);
		
		String ingredientsToDB = "";
		String name = nameTextView.getText().toString();
		String approach = approachTextView.getText().toString();
		
		int selected = radiogroup.getCheckedRadioButtonId();
		RadioButton b = (RadioButton) findViewById(selected);
		String category = b.getText().toString();
		
		if(category.equals("Main"))
			category = category + " Course";
		
		for (int i=0; i<ingredientsTextView.getCount(); i++)
        	ingredientsToDB += ingredientsTextView.getItemAtPosition(i)+";";
		
		if(name.length() == 0 || approach.length() == 0 || ingredientsToDB.length() == 0 )
        	Toast.makeText(this, "Please fill out all fields!", Toast.LENGTH_SHORT).show();
		else
		{
	        ingredientsToDB = ingredientsToDB.substring(0, ingredientsToDB.length()-1);
	        
			r.setCategory(category);
			r.setApproch(approach);
			r.setName(name);
			r.setIngredients(ingredientsToDB);
			//String pis = db.getImageUrl(name);
			//if(pis.equals(""))
			r.setImagePath(imageUri.toString());
			r.setID(id);
			
			
			if(modify.equals("ModifyRecipe"))
			{
				db.updateRecipe(r);
				Toast.makeText(this, "Recipe updated", Toast.LENGTH_SHORT).show();
			}
			else
			{
				db.addRecipe(r);
				Toast.makeText(this, "Recipe created", Toast.LENGTH_SHORT).show();
			}
			// update af alle fragments - textview og listview
			ImageFragment.textUpdateImage(r);
			ApproachFragment.textUpdateApproach(r);
			IngredientFragment.textUpdateIngredient(r);
			
			
			
		}	
	}	
}
